package com.simplehire.util;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

public class FileContentExtractor {

    // 主提取方法，根据文件类型调用对应解析器
    public static String extractContent(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        if (fileName == null) {
            throw new IOException("文件名称为空");
        }

        try (InputStream inputStream = file.getInputStream()) {
            String lowerCaseFileName = fileName.toLowerCase();
            if (lowerCaseFileName.endsWith(".pdf")) {
                return extractPdfContent(inputStream);
            } else if (lowerCaseFileName.endsWith(".doc")) {
                return extractDocContent(inputStream);
            } else if (lowerCaseFileName.endsWith(".docx")) {
                return extractDocxContent(inputStream);
            } else if (lowerCaseFileName.endsWith(".txt")) {
                return new String(inputStream.readAllBytes());
            } else {
                throw new IOException("不支持的文件类型: " + fileName);
            }
        }
    }

    /**
     * 提取PDF内容 - PDFBox 3.x 版本
     */
    private static String extractPdfContent(InputStream inputStream) throws IOException {
        try (PDDocument document = Loader.loadPDF(inputStream.readAllBytes())) {
            PDFTextStripper stripper = new PDFTextStripper();
            stripper.setSortByPosition(true);
            stripper.setAddMoreFormatting(true);
            return stripper.getText(document);
        } catch (IOException e) {
            throw new IOException("解析PDF文件失败: " + e.getMessage(), e);
        } catch (Exception e) {
            throw new IOException("处理PDF文件时发生错误: " + e.getMessage(), e);
        }
    }

    // 其他方法保持不变...
    /**
     * 提取旧版DOC内容
     */
    private static String extractDocContent(InputStream inputStream) throws IOException {
        try (HWPFDocument document = new HWPFDocument(inputStream)) {
            WordExtractor extractor = new WordExtractor(document);
            return extractor.getText();
        } catch (IOException e) {
            throw new IOException("解析DOC文件失败: " + e.getMessage(), e);
        } catch (Exception e) {
            throw new IOException("处理DOC文件时发生错误: " + e.getMessage(), e);
        }
    }

    /**
     * 提取新版DOCX内容
     */
    private static String extractDocxContent(InputStream inputStream) throws IOException {
        try (XWPFDocument document = new XWPFDocument(inputStream)) {
            XWPFWordExtractor extractor = new XWPFWordExtractor(document);
            return extractor.getText();
        } catch (IOException e) {
            throw new IOException("解析DOCX文件失败: " + e.getMessage(), e);
        } catch (Exception e) {
            throw new IOException("处理DOCX文件时发生错误: " + e.getMessage(), e);
        }
    }
}